package com.wandian.weather.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.CornerPathEffect;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathEffect;
import android.graphics.RectF;
import android.text.TextUtils;
import android.view.View;

import androidx.core.content.ContextCompat;

import com.wandian.weather.R;
import com.wandian.weather.bean.TianqiWeather30Bean;

import java.util.List;

import q.rorbin.verticaltablayout.util.DisplayUtil;


public class CustomCurveChart extends View {

    // 坐标单位
    private String[] xLabel;
    private String[] yLabel;
    // 曲线数据
    private List<Float> dataList;
    // 默认边距
    private int margin = 40;
    // 距离左边偏移量
    private int marginX = 50;
    // 原点坐标
    private int xPoint;
    private int yPoint;
    // X,Y轴的单位长度
    private int xScale;
    private int yScale;
    // 画笔
    private Paint paintAxes;
    private Paint paintCoordinate;
    private Paint paintTable;
    private Paint paintCurve;
    private Paint paintRectF;
    private Paint paintValue;

    private float minY;
    private float maxY;
    private Context context;

    TianqiWeather30Bean.LowtempBean.DataBean.CdateBean lowCdate;
    TianqiWeather30Bean.UptempBean.DataBeanX.CdateBeanX upCdate;

    public CustomCurveChart(Context context, String[] xLabel, String[] yLabel,
                            List<Float> dataList, float maxY, float minY,
                            TianqiWeather30Bean.UptempBean.DataBeanX.CdateBeanX upCdate,
                            TianqiWeather30Bean.LowtempBean.DataBean.CdateBean lowCdate) {
        super(context);
        this.xLabel = xLabel;
        this.yLabel = yLabel;
        this.dataList = dataList;
        this.maxY = maxY;
        this.minY = minY;
        this.context = context;
        this.upCdate = upCdate;
        this.lowCdate = lowCdate;
    }

    public CustomCurveChart(Context context) {
        super(context);
    }

    /**
     * 初始化数据值和画笔
     */
    public void init() {
        xPoint = margin + marginX;
        yPoint = this.getHeight() - margin;
        xScale = (this.getWidth() - 2 * margin - marginX) / (xLabel.length - 1);
//        yScale = (this.getHeight() - 2 * margin) / (yLabel.length - 1);
        yScale = (int) ((this.getHeight() - 2 * margin) / (maxY - minY));

        paintAxes = new Paint();
        paintAxes.setStyle(Paint.Style.STROKE);
        paintAxes.setAntiAlias(true);
        paintAxes.setDither(true);
        paintAxes.setColor(Color.parseColor("#50FFFFFF"));
        paintAxes.setStrokeWidth(4);

        paintCoordinate = new Paint();
        paintCoordinate.setStyle(Paint.Style.STROKE);
        paintCoordinate.setDither(true);
        paintCoordinate.setAntiAlias(true);
        paintCoordinate.setColor(Color.parseColor("#FFFFFF"));
        paintCoordinate.setTextSize(DisplayUtil.dp2px(context, 12f));

        paintTable = new Paint();
        paintTable.setStyle(Paint.Style.STROKE);
        paintTable.setAntiAlias(true);
//        paintTable.setDither(true);
        paintTable.setColor(Color.parseColor("#50FFFFFF"));
        paintTable.setStrokeWidth(2);
        paintTable.setPathEffect(new DashPathEffect(new float[]{4, 4}, 0));

        paintCurve = new Paint();
        paintCurve.setStyle(Paint.Style.STROKE);
        paintCurve.setDither(true);
        paintCurve.setAntiAlias(true);
        paintCurve.setStrokeWidth(3);
        PathEffect pathEffect = new CornerPathEffect(25);
        paintCurve.setPathEffect(pathEffect);

        paintRectF = new Paint();
        paintRectF.setStyle(Paint.Style.FILL);
        paintRectF.setDither(true);
        paintRectF.setAntiAlias(true);
        paintRectF.setStrokeWidth(3);

        paintValue = new Paint();
        paintValue.setStyle(Paint.Style.STROKE);
        paintValue.setAntiAlias(true);
        paintValue.setDither(true);
        paintValue.setColor(ContextCompat.getColor(getContext(), R.color.yellow));
        paintValue.setTextAlign(Paint.Align.CENTER);
        paintValue.setTextSize(DisplayUtil.dp2px(context, 12f));
    }

    @Override
    protected void onDraw(Canvas canvas) {
//        canvas.drawColor(ContextCompat.getColor(getContext(), R.color.color1));
        try {
            init();
            drawTable(canvas, paintTable);
            drawAxesLine(canvas, paintAxes);
            drawCoordinate(canvas, paintCoordinate);
            drawCurve(canvas, paintCurve, dataList);
            drawValue(canvas, paintRectF, dataList);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 绘制坐标轴
     */
    private void drawAxesLine(Canvas canvas, Paint paint) {
        // X
        canvas.drawLine(xPoint, yPoint, this.getWidth() - margin / 6, yPoint, paint);
//        canvas.drawLine(this.getWidth() - margin / 6, yPoint, this.getWidth() - margin / 2, yPoint - margin / 3, paint);
//        canvas.drawLine(this.getWidth() - margin / 6, yPoint, this.getWidth() - margin / 2, yPoint + margin / 3, paint);

        // Y
        canvas.drawLine(xPoint, yPoint, xPoint, margin / 6, paint);
//        canvas.drawLine(xPoint, margin / 6, xPoint - margin / 3, margin / 2, paint);
//        canvas.drawLine(xPoint, margin / 6, xPoint + margin / 3, margin / 2, paint);
    }

    /**
     * 绘制表格
     */
    private void drawTable(Canvas canvas, Paint paint) {
        // 横向线
//        for (int i = 1; (yPoint - i * yScale) >= margin; i++) {
//            int startX = xPoint;
//            int startY = yPoint - i * yScale;
//            int stopX = xPoint + (xLabel.length - 1) * xScale;
//            path.moveTo(startX, startY);
//            path.lineTo(stopX, startY);
//            canvas.drawPath(path, paint);
//        }

        float upStartX = 0;
        float upEndX = 0;
        float lowStartX = 0;
        float lowEndX = 0;
        // 纵向线
        for (int i = 1; i < xLabel.length; i++) {
            if (upCdate != null) {
                if (TextUtils.equals(xLabel[i], upCdate.getStart_date()) || TextUtils.equals(xLabel[i], upCdate.getEnd_date())) {
                    Path path = new Path();
                    int startX = xPoint + i * xScale;
                    int startY = yPoint;
                    float stopY = toY(dataList.get(i));
                    path.moveTo(startX, startY);
                    path.lineTo(startX, stopY);
                    canvas.drawPath(path, paint);

                    if (TextUtils.equals(xLabel[i], upCdate.getStart_date()))
                        upStartX = startX;
                    if (TextUtils.equals(xLabel[i], upCdate.getEnd_date()))
                        upEndX = startX;
                }
            }

            if (lowCdate != null) {
                if (TextUtils.equals(xLabel[i], lowCdate.getStart_date()) || TextUtils.equals(xLabel[i], lowCdate.getEnd_date())) {
                    Path path = new Path();
                    int startX = xPoint + i * xScale;
                    int startY = yPoint;
                    float stopY = toY(dataList.get(i));
                    path.moveTo(startX, startY);
                    path.lineTo(startX, stopY);
                    canvas.drawPath(path, paint);

                    if (TextUtils.equals(xLabel[i], lowCdate.getStart_date()))
                        lowStartX = startX;
                    if (TextUtils.equals(xLabel[i], lowCdate.getEnd_date()))
                        lowEndX = startX;
                }
            }
        }

        Paint p = new Paint();
        p.setStyle(Paint.Style.STROKE);
        p.setDither(true);
        p.setAntiAlias(true);
        p.setColor(Color.parseColor("#FFFFFF"));
        p.setTextSize(DisplayUtil.dp2px(context, 12f));

        float font = DisplayUtil.dp2px(context, 14f);
        float x1 = (upEndX - upStartX) / 2 + upStartX - font;
        if (upCdate != null)
            canvas.drawText("升温", x1 + 10, yPoint + font - 5, p);

        float x2 = (lowEndX - lowStartX) / 2 + lowStartX - font;
        if (lowCdate != null)
            canvas.drawText("降温", x2 + 10, yPoint + font - 5, p);
    }

    /**
     * 绘制刻度
     */
    private void drawCoordinate(Canvas canvas, Paint paint) {
        // X轴坐标
        for (int i = 0; i <= (xLabel.length - 1); i++) {
            paint.setTextAlign(Paint.Align.CENTER);
            int startX = xPoint + i * xScale;
            if (i == 0 || i == xLabel.length - 1)
                canvas.drawText(xLabel[i], startX, this.getHeight() - margin / 6, paint);
        }

        // Y轴坐标
        for (int i = 0; i <= (yLabel.length - 1); i++) {
            paint.setTextAlign(Paint.Align.LEFT);
            float startY = yPoint;
            if (i == 0)
                startY = yPoint - (minY + 5 - minY) * yScale;
            else if (i == 1)
                startY = yPoint - (maxY - 5 - minY) * yScale;

            int offsetX;
            switch (yLabel[i].length()) {
                case 1:
                    offsetX = 28;
                    break;

                case 2:
                    offsetX = 20;
                    break;

                case 3:
                    offsetX = 12;
                    break;

                case 4:
                    offsetX = 5;
                    break;

                default:
                    offsetX = 0;
                    break;
            }
            int offsetY;
            if (i == 0) {
                offsetY = 0;
            } else {
                offsetY = margin / 5;
            }
            // x默认是字符串的左边在屏幕的位置，y默认是字符串是字符串的baseline在屏幕上的位置
            canvas.drawText(yLabel[i], margin / 4 + offsetX, startY + offsetY, paint);
        }
    }

    /**
     * 绘制曲线
     */
    private void drawCurve(Canvas canvas, Paint paint, List<Float> data) {
        paint.setColor(ContextCompat.getColor(getContext(), R.color.white));
        Path path = new Path();
        for (int i = 0; i <= (xLabel.length - 1); i++) {
            if (i == 0) {
                path.moveTo(xPoint, toY(data.get(0)));
            } else {
                path.lineTo(xPoint + i * xScale, toY(data.get(i)));
            }

            if (i == xLabel.length - 1) {
                path.lineTo(xPoint + i * xScale, toY(data.get(i)));
            }

        }
        canvas.drawPath(path, paint);
    }

    /**
     * 绘制数值
     */
    private void drawValue(Canvas canvas, Paint paint, List<Float> data) {
        float font = DisplayUtil.dp2px(context, 12f);
        for (int i = 1; i <= (xLabel.length - 1); i++) {
            RectF rect;
            if (toY(data.get(i - 1)) < toY(data.get(i))) {
                rect = new RectF(xPoint + i * xScale - 20, toY(data.get(i)) - 15,
                        xPoint + i * xScale + 20, toY(data.get(i)) + 5);

                if (data.get(i) == maxY - 5 || data.get(i) == minY + 5) {
                    if (data.get(i) == maxY - 5) {
                        paint.setColor(Color.parseColor("#FF4457"));
                        paintValue.setColor(Color.parseColor("#FF4457"));
                        canvas.drawText(data.get(i) + "°", rect.left + font, rect.top - 10, paintValue);
                    } else {
                        paint.setColor(Color.parseColor("#35B5F8"));
                        paintValue.setColor(Color.parseColor("#35B5F8"));
                        canvas.drawText(data.get(i) + "°", rect.left + font, rect.top + font + 20, paintValue);
                    }
                    canvas.drawCircle(rect.left + rect.width() / 2, rect.top + rect.height() / 2, 10, paint);

                }

            } else if (toY(data.get(i - 1)) > toY(data.get(i))) {
                rect = new RectF(xPoint + i * xScale - 20, toY(data.get(i)) - 5,
                        xPoint + i * xScale + 20, toY(data.get(i)) + 15);
                if (data.get(i) == maxY - 5 || data.get(i) == minY + 5) {
                    if (data.get(i) == maxY - 5) {
                        paint.setColor(Color.parseColor("#FF4457"));
                        paintValue.setColor(Color.parseColor("#FF4457"));
                        canvas.drawText(data.get(i) + "°", rect.left + font, rect.top - 10, paintValue);
                    } else {
                        paint.setColor(Color.parseColor("#35B5F8"));
                        paintValue.setColor(Color.parseColor("#35B5F8"));
                        canvas.drawText(data.get(i) + "°", rect.left + font, rect.top + font + 20, paintValue);
                    }
                    canvas.drawCircle(rect.left + rect.width() / 2, rect.top + rect.height() / 2, 10, paint);
                }

            } else {
//                rect = new RectF(xPoint + i * xScale - 20, toY(data.get(i)) - 10,
//                        xPoint + i * xScale + 20, toY(data.get(i)) + 10);
//                if (data.get(i) == maxY - 5 || data.get(i) == minY + 5) {
//                    if (data.get(i) == maxY - 5) {
//                        paint.setColor(Color.parseColor("#FF4457"));
//                        paintValue.setColor(Color.parseColor("#FF4457"));
//                        canvas.drawText(data.get(i) + "°", rect.left + font, rect.top - 10, paintValue);
//                    } else {
//                        paint.setColor(Color.parseColor("#35B5F8"));
//                        paintValue.setColor(Color.parseColor("#35B5F8"));
//                        canvas.drawText(data.get(i) + "°", rect.left + font, rect.top + font + 20, paintValue);
//                    }
//                    canvas.drawCircle(rect.left + rect.width() / 2, rect.top + rect.height() / 2, 10, paint);
//                }
            }
        }
    }

    /**
     * 数据按比例转坐标
     */
    private float toY(float num) {
        float y;
        try {
//            float a = (float) num / 100.0f;
            y = yPoint - (num - minY) * yScale;
        } catch (Exception e) {
            return 0;
        }
        return y;
    }
}
